import { createPinia } from 'pinia'
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import {zhCn} from "element-plus/es/locale/index";
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import { useAuthStore } from './store/auth';

const app = createApp(App)

app.use(ElementPlus, { size: 'small', zIndex: 3000,locale:zhCn })
const pinia = createPinia()
app.use(pinia)

// 先初始化Pinia
const authStore = useAuthStore();

// 确保从localStorage加载用户信息
authStore.loadFromLocalStorage();

// 设置路由守卫确保登录状态在页面切换时保持
router.beforeEach((to, from, next) => {
  console.log('路由切换，当前登录状态:', authStore.isLoggedIn);
  console.log('localStorage中的用户信息:', {
    token: localStorage.getItem('token'),
    user_id: localStorage.getItem('user_id'),
    user_type: localStorage.getItem('user_type')
  });
  
  // 在这里可以添加路由权限控制逻辑
  next();
});

for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}
app.use(router)
app.mount('#app')